//
//  ContactSegmentView.swift
//  koolcar
//
//  Created by xiantian on 2023/11/2.
//

import UIKit
typealias ContactSegmentViewBlock = (NSInteger)->Void
class ContactSegmentView: UIView {
    var callBack: ContactSegmentViewBlock?
    private var collectionView: UICollectionView!
    private var selectIndex: NSInteger = 0
    private var dataArray: [String] = []
    
    init(frame: CGRect, titles: [String]? = nil) {
        super.init(frame: frame)
        self.dataArray = titles ?? []
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .horizontal
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 14, y: 0, width: frame.size.width - 28, height: frame.size.height), collectionViewLayout: layout)
        self.collectionView.register(ContactSegmentItemView.self, forCellWithReuseIdentifier: "ContactSegmentItemView")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = UIColor.white
        self.collectionView.isScrollEnabled = false
        self.collectionView.showsHorizontalScrollIndicator = false
        self.addSubview(self.collectionView)
    }
    
    public func setContactSegmentView(titles: [String]){
        self.dataArray = titles
        self.collectionView.reloadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ContactSegmentView: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if self.dataArray.count == 0 {
            return CGSize.init(width: self.frame.size.width, height: self.collectionView.frame.size.height)
        }
        return  CGSize.init(width:  self.frame.size.width/self.dataArray.count, height: frame.size.height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContactSegmentItemView", for: indexPath) as! ContactSegmentItemView
        //        switch indexPath.row {
        //            case 0:
        //                cell.titleLab.text = MMLocalizedString("", comment: "公司介绍")
        //            case 1:
        //                cell.titleLab.text = MMLocalizedString("", comment: "公司优势")
        //            case 2:
        //                cell.titleLab.text = MMLocalizedString("", comment: "专业汽车代购")
        //            case 4:
        //                cell.titleLab.text = MMLocalizedString("", comment: "自定义标题")
        //            default:
        //                ()
        //        }
        if self.dataArray.count > indexPath.row {
            cell.titleLab.text = self.dataArray[indexPath.row]
        }
        
        if indexPath.row == self.selectIndex {
            cell.titleLab.textColor = Theme.mainColor
            cell.titleLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
            cell.arrowImgView.isHidden = false
        }else{
            cell.titleLab.textColor = Theme.titleLabColor
            cell.titleLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
            cell.arrowImgView.isHidden = true
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectIndex = indexPath.row
        self.collectionView.reloadData()
        self.callBack?(self.selectIndex)
    }
}



class ContactSegmentItemView: UICollectionViewCell {
    var titleLab: UILabel!
    var arrowImgView: UIImageView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        titleLab = UILabel()
        titleLab.textColor = Theme.titleLabColor
        titleLab.backgroundColor = UIColor.clear
        titleLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        titleLab.numberOfLines = 1
        titleLab.lineBreakMode = .byTruncatingTail
        titleLab.textAlignment = .center
        contentView.addSubview(titleLab)
        
        arrowImgView = UIImageView()
        arrowImgView.image = UIImage.init(named: "navi_select")
        arrowImgView.contentMode = .scaleAspectFit
        contentView.addSubview(arrowImgView)
        
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(10)
        }
        
        arrowImgView.snp.makeConstraints { make in
            make.width.equalTo(20)
            make.height.equalTo(8)
            make.centerX.equalTo(titleLab.snp.centerX)
            make.top.equalTo(titleLab.snp.bottom)
            make.bottom.equalTo(-10)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
